package com.zhentao.service.impl;


import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zhentao.common.enums.admin.AdNotice;
import com.zhentao.common.enums.dto.AdNoticeDto;
import com.zhentao.dtos.Result;
import com.zhentao.mapper.AdNoticeMapper;
import com.zhentao.service.NoticeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceimpl extends ServiceImpl<AdNoticeMapper,AdNotice> implements NoticeService {
    @Autowired
    private AdNoticeMapper adNoticeMapper;
    @Override
    public List<AdNotice> list() {
        List<AdNotice> noticelist = adNoticeMapper.noticelist();
        return noticelist;
    }

    /**
     * 公告列表展示
     * @param adNoticeDto
     * @return
     */
    @Override
    public Result noticeList(AdNoticeDto adNoticeDto) {
        Page<AdNotice> page = new Page<>(adNoticeDto.getPage(), adNoticeDto.getSize());

        LambdaQueryWrapper<AdNotice> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(adNoticeDto.getContent()), AdNotice::getContent, adNoticeDto.getContent());

        Page<AdNotice> adNoticePage = adNoticeMapper.selectPage(page, wrapper);

        return Result.success("操作成功").put("total", page.getTotal()).put("data", adNoticePage.getRecords());
    }

    /**
     * 公告新增或修改
     * @param adNotice
     * @return
     */
    @Override
    public Result noticeAddOrUpd(AdNotice adNotice) {
        if (adNotice.getId() == null) {
            adNotice.setIsDelete(1);
            adNotice.setStatus(0);
            adNotice.setContent(adNotice.getContent().trim());
            adNotice.setCreateBy(adNotice.getCreateBy());
            adNotice.setCreateTime(new Date());
        } else if (adNotice.getId() !=null) {
            adNotice.setIsDelete(adNotice.getIsDelete());
            adNotice.setStatus(adNotice.getStatus());
            adNotice.setContent(adNotice.getContent().trim());
            adNotice.setUpdateTime(new Date());
        }
        this.saveOrUpdate(adNotice);
        return Result.success("操作成功");
    }


    /**
     * 公告删除（逻辑删除）
     * @param id
     * @return
     */
    @Override
    public Result noticeDelete(Integer id) {
        boolean b = this.removeById(id);
        if (b){
            return Result.success("操作成功");
        }else {
            return Result.error("请选择要删除数据或无需重复操作");
        }
    }
}
